<?php
declare(strict_types=1);

namespace WebApp\lib\db;

use WebApp\lib\util\Util;

/**
 * Class AbstractTable
 * @package txz\common\third_lib\model\lib
 */
abstract class AbstractTable
{
    public $server_name = null;
    public $db_name = null;
    public $table_name = null;
    public $pre = null;
    public $suffix = null;
    /**
     * @var Pool
     */
    public $pool;

    public function __construct(string $server_name, string $db_name, string $table_name, string $pre = '', string $suffix = '')
    {
        $namespace_arr = explode('\\', $server_name);
        $server_name = end($namespace_arr);

        $class_arr = explode('\\', $table_name);
        $table_name = end($class_arr);
        $table_name = str_replace("Model", "", $table_name);
        $table_name = lcfirst($table_name);
        $tmp = '';
        $str_len = strlen($table_name);
        for ($i = 0; $i < $str_len; $i++) {
            $code = ord($table_name[$i]);
            if ($code > 64 && $code < 91) {
                $tmp .= '_' . chr($code + 32);
            } else {
                $tmp .= $table_name[$i];
            }
        }
//        $l = strlen($tmp) - 6;
//        if (substr($tmp, $l, 6) === "_model") {
//            $tmp = substr($tmp, 0, $l);
//        }
        $table_name = $tmp;

        //init
        {
            $this->pre = $pre;
            $this->suffix = ($suffix);
            $this->db_name = ($db_name);
            $this->server_name = ($server_name);
            $this->table_name = (
                ($pre ? $pre . '_' : '') .
                $table_name .
                ($suffix ? '_' . $suffix : '')
            );
        }

//        $this->process($server_name, $db_name, $tmp, $pre, $suffix);
    }

    abstract function getPool();

    /**
     * @param $server_name
     * @param $db_name
     * @param $table_name
     * @param string $pre
     * @param string $suffix
     */
    private function init(string $server_name, string $db_name, string $table_name, string $pre = '', string $suffix = ''): void
    {
//        $this->init_params = [$db_name, $table_name, $pre, $suffix];
        $namespace_arr = explode('\\', $server_name);
        $server_name = end($namespace_arr);

//        if ($count - 2 < 0) {
//            $server_name = '_204';
//        } else {
//            $server_name = $namespace_arr[$count - 2];
//        }

        $class_arr = explode('\\', $table_name);
        $table_name = $class_arr[count($class_arr) - 1];

        $table_name = lcfirst($table_name);
        $tmp = '';
        $str_len = strlen($table_name);
        for ($i = 0; $i < $str_len; $i++) {
            $code = ord($table_name[$i]);
            if ($code > 64 && $code < 91) {
                $tmp .= '_' . chr($code + 32);
            } else {
                $tmp .= $table_name[$i];
            }
        }

        $l = strlen($tmp) - 6;
        if (substr($tmp, $l, 6) === "_model") {
            $tmp = substr($tmp, 0, $l);
        }

        $this->process($server_name, $db_name, $tmp, $pre, $suffix);
    }

    /**
     * @param $server
     * @param $dbName
     * @param $tableName
     * @param string $pre
     * @param string $suffix
     * @return mixed
     */
    public function process($server, $dbName, $tableName, $pre = '', $suffix = ''): void
    {
        $this->pre = $pre;
        $this->suffix = ($suffix);
        $this->db_name = ($dbName);
        $this->server_name = ($server);
        $this->table_name = (
            ($pre ? $pre . '_' : '') .
            $tableName .
            ($suffix ? '_' . $suffix : '')
        );
    }

    /**
     * @return null
     */
    public function getDbName()
    {
        return $this->db_name;
    }

    /**
     * @param null $db_name
     */
    public function setDbName($db_name)
    {
        $this->db_name = $db_name;
    }

    /**
     * @return null
     */
    public function getTableName()
    {
        return $this->table_name;
    }

    /**
     * @param null $table_name
     */
    public function setTableName($table_name)
    {
        $this->table_name = $table_name;
    }

    /**
     * @return null
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * @param null $pre
     */
    public function setPre($pre)
    {
        $this->pre = $pre;
    }

    /**
     * @return null
     */
    public function getSuffix()
    {
        return $this->suffix;
    }

    /**
     * @param null $suffix
     */
    public function setSuffix($suffix)
    {
        $this->suffix = $suffix;
    }

    /**
     * @return null
     */
    public function getServerName()
    {
        return $this->server_name;
    }

    /**
     * @param null $server_name
     */
    public function setServerName($server_name): void
    {
        $this->server_name = $server_name;
    }
}
